package com.ruoyi.web.controller.gxs;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.gxs.domain.GxsECharts;
import com.ruoyi.gxs.domain.GxsPendActivity;
import com.ruoyi.gxs.domain.GxsPendAward;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gxs.domain.GxsDeduction;
import com.ruoyi.gxs.service.IGxsDeductionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 减分Controller
 * 
 * @author 高晓松
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/gxs/deduction")
public class GxsDeductionController extends BaseController
{
    @Autowired
    private IGxsDeductionService gxsDeductionService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询减分列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:deduction:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxsDeduction gxsDeduction)
    {
        startPage();
        List<GxsDeduction> list = gxsDeductionService.selectGxsDeductionList(gxsDeduction);
        return getDataTable(list);
    }

    /**
     * 导出减分列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:deduction:export')")
    @Log(title = "减分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxsDeduction gxsDeduction)
    {
        List<GxsDeduction> list = gxsDeductionService.selectGxsDeductionList(gxsDeduction);
        ExcelUtil<GxsDeduction> util = new ExcelUtil<GxsDeduction>(GxsDeduction.class);
        util.exportExcel(response, list, "减分数据");
    }

    /**
     * 获取减分详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gxs:deduction:query')")
    @GetMapping(value = "/{gxsId}")
    public AjaxResult getInfo(@PathVariable("gxsId") Long gxsId)
    {
        return success(gxsDeductionService.selectGxsDeductionByGxsId(gxsId));
    }

    /**
     * 新增减分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:deduction:add')")
    @Log(title = "减分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxsDeduction gxsDeduction)
    {
//        //用户id、学号、部门id
//        SysUser sysUser = sysUserService.selectUserById(gxsDeduction.getUserId());
//        gxsDeduction.setUserId(sysUser.getUserId());
//        gxsDeduction.setUserName(sysUser.getUserName());
//        gxsDeduction.setDeptId(sysUser.getDeptId());
        String userName = gxsDeduction.getUserName();
        SysUser user = sysUserService.selectUserByUserName(userName);

        gxsDeduction.setUserId(user.getUserId());
        gxsDeduction.setUserName(user.getUserName());
        gxsDeduction.setDeptId(user.getDeptId());

        //创建者和创建时间
        gxsDeduction.setCreateBy(getUsername());
        gxsDeduction.setCreateTime(new Date());
        return toAjax(gxsDeductionService.insertGxsDeduction(gxsDeduction));
    }

    /**
     * 修改减分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:deduction:edit')")
    @Log(title = "减分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxsDeduction gxsDeduction)
    {
        gxsDeduction.setUpdateBy(getUsername());
        gxsDeduction.setUpdateTime(new Date());
        return toAjax(gxsDeductionService.updateGxsDeduction(gxsDeduction));
    }

    /**
     * 删除减分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:deduction:remove')")
    @Log(title = "减分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gxsIds}")
    public AjaxResult remove(@PathVariable Long[] gxsIds)
    {
        return toAjax(gxsDeductionService.deleteGxsDeductionByGxsIds(gxsIds));
    }

    /**
     * 查询我的减分列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:deduction:list')")
    @GetMapping("/MyList")
    public TableDataInfo MyList(GxsDeduction gxsDeduction)
    {
        startPage();
        List<GxsDeduction> list = gxsDeductionService.selectGxsDeductionList(gxsDeduction);
        String username = getUsername();
        for (int i = 0 ; i < list.size(); i++) {
            if (!list.get(i).getUserName().equals(username) ) {
                list.remove(i);
                i--;
            }
        }
        return getDataTable(list);
    }


    /**
     * 本级活动分布
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:list')")
    @GetMapping("/chartList")
    public TableDataInfo chartList(GxsDeduction gxsDeduction)
    {
        startPage();

        List<GxsDeduction> deductionList = gxsDeductionService.selectGxsDeductionList(gxsDeduction);

        List<GxsECharts> list = new ArrayList<>();
        GxsECharts eCharts0 = new GxsECharts();
        GxsECharts eCharts1 = new GxsECharts();
        GxsECharts eCharts2 = new GxsECharts();

        eCharts0.setType("专业技能");
        eCharts0.setNum(0);;
        list.add(0,eCharts0);
        eCharts1.setType("思想品德");
        eCharts1.setNum(0);;
        list.add(1, eCharts1);
        eCharts2.setNum(0);;
        eCharts2.setType("体美劳");
        list.add(2,eCharts2);

        //筛选本级活动
        Long deptId = getDeptId();


        for (int i = 0 ; i < deductionList.size(); i++){
            Long gxsTypeId = deductionList.get(i).getGxsParenttypeId();
            switch ( gxsTypeId.intValue()){
                case 1:
                    Integer num0 = list.get(0).getNum();
                    list.get(0).setNum( num0 + 1);
                    break;
                case 2:
                    Integer num1 = list.get(1).getNum();
                    list.get(1).setNum( num1 + 1);
                    break;
                case 3:
                    Integer num2 = list.get(2).getNum();
                    list.get(2).setNum( num2 + 1);
                    break;
            }
        }

        return getDataTable(list);
    }
}
